//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CallSpecifiedMultiProduct.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Multistep;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProduct.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::CCallSpecifiedMultiProduct (boost::shared_ptr<QuantLib::CallSpecifiedMultiProduct>& childNative, Object^ owner) : CMarketModelMultiProduct(CCallSpecifiedMultiProduct::typeid)
{
#ifdef HANDLE
	_phCallSpecifiedMultiProduct = NULL;
#endif
	_ppCallSpecifiedMultiProduct = &childNative;
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppCallSpecifiedMultiProduct));
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::CCallSpecifiedMultiProduct (QuantLib::CallSpecifiedMultiProduct& childNative, Object^ owner) : CMarketModelMultiProduct(CCallSpecifiedMultiProduct::typeid)
{
#ifdef HANDLE
	_phCallSpecifiedMultiProduct = NULL;
#endif
	_ppCallSpecifiedMultiProduct = new boost::shared_ptr<QuantLib::CallSpecifiedMultiProduct> (&childNative);
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppCallSpecifiedMultiProduct));
    _CallSpecifiedMultiProductOwner = owner;
    _MarketModelMultiProductOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::CCallSpecifiedMultiProduct (CCallSpecifiedMultiProduct^ copy) : CMarketModelMultiProduct(CCallSpecifiedMultiProduct::typeid)
{
#ifdef HANDLE
	_phCallSpecifiedMultiProduct = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCallSpecifiedMultiProduct = new boost::shared_ptr<QuantLib::CallSpecifiedMultiProduct> (copy->GetShared());
        _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppCallSpecifiedMultiProduct));
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::CCallSpecifiedMultiProduct (System::Type^ t) : CMarketModelMultiProduct(CCallSpecifiedMultiProduct::typeid)
{
#ifdef HANDLE
	_phCallSpecifiedMultiProduct = NULL;
#endif
	if (!t->IsSubclassOf(CCallSpecifiedMultiProduct::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::CCallSpecifiedMultiProduct (QuantLib::Handle<QuantLib::CallSpecifiedMultiProduct>& childNative, Object^ owner)  : CMarketModelMultiProduct(CCallSpecifiedMultiProduct::typeid)
{
	_phCallSpecifiedMultiProduct = &childNative;
	_ppCallSpecifiedMultiProduct = &static_cast<boost::shared_ptr<QuantLib::CallSpecifiedMultiProduct>>(childNative.currentLink());
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppCallSpecifiedMultiProduct));
    _CallSpecifiedMultiProductOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::CCallSpecifiedMultiProduct (QuantLib::Handle<QuantLib::CallSpecifiedMultiProduct> childNative)  : CMarketModelMultiProduct(CCallSpecifiedMultiProduct::typeid)
{
	_phCallSpecifiedMultiProduct = &childNative;
	_ppCallSpecifiedMultiProduct = &static_cast<boost::shared_ptr<QuantLib::CallSpecifiedMultiProduct>>(childNative.currentLink());
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppCallSpecifiedMultiProduct));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::CCallSpecifiedMultiProduct (QuantLib::CallSpecifiedMultiProduct childNative)  : CMarketModelMultiProduct(CCallSpecifiedMultiProduct::typeid)
{
#ifdef HANDLE
	_phCallSpecifiedMultiProduct = NULL;
#endif
	_ppCallSpecifiedMultiProduct = new boost::shared_ptr<QuantLib::CallSpecifiedMultiProduct> (new QuantLib::CallSpecifiedMultiProduct (childNative));
    _ppMarketModelMultiProduct = new boost::shared_ptr<QuantLib::MarketModelMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelMultiProduct> (*_ppCallSpecifiedMultiProduct));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::~CCallSpecifiedMultiProduct ()
{
    if (_ppCallSpecifiedMultiProduct != NULL)
    {
	    delete _ppCallSpecifiedMultiProduct;
        _ppCallSpecifiedMultiProduct = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::!CCallSpecifiedMultiProduct ()
{
    if (_ppCallSpecifiedMultiProduct != NULL)
    {
	    delete _ppCallSpecifiedMultiProduct;
    }
}
QuantLib::CallSpecifiedMultiProduct& Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::GetReference ()
{
    if (_ppCallSpecifiedMultiProduct == NULL) throw gcnew NativeNullException ();
	return **_ppCallSpecifiedMultiProduct;
}
boost::shared_ptr<QuantLib::CallSpecifiedMultiProduct>& Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::GetShared ()
{
    if (_ppCallSpecifiedMultiProduct == NULL) throw gcnew NativeNullException ();
	return *_ppCallSpecifiedMultiProduct;
}
QuantLib::CallSpecifiedMultiProduct* Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::GetPointer ()
{
    if (_ppCallSpecifiedMultiProduct == NULL) throw gcnew NativeNullException ();
	return &**_ppCallSpecifiedMultiProduct;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CallSpecifiedMultiProduct>& Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::GetHandle ()
{
	if (_phCallSpecifiedMultiProduct == NULL)
	{
		_phCallSpecifiedMultiProduct = new Handle<QuantLib::CallSpecifiedMultiProduct> (*_ppCallSpecifiedMultiProduct);
	}
	return *_phCallSpecifiedMultiProduct;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::HasNative () 
{
	return (_ppCallSpecifiedMultiProduct != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::NextTimeStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState, Cephei::IVector<UInt64>^ numberCashFlowsThisStep, Cephei::IMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ cashFlowsGenerated)
{
    CCurveState^ _CcurrentState;
    CoVector<UInt64>^ _CnumberCashFlowsThisStep;
    CoMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _CcashFlowsGenerated;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
        _CnumberCashFlowsThisStep = safe_cast<CoVector<UInt64>^> (numberCashFlowsThisStep);
        _CnumberCashFlowsThisStep->Lock();
        INativeVector<UInt64>^ _NCInumberCashFlowsThisStep = _CnumberCashFlowsThisStep->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCnumberCashFlowsThisStep = safe_cast<CUInt64Vector^>(_NCInumberCashFlowsThisStep);
        std::vector<QuantLib::Size>& _numberCashFlowsThisStep = static_cast<std::vector<QuantLib::Size>&> (_NCnumberCashFlowsThisStep->GetReference ());
        _CcashFlowsGenerated = safe_cast<CoMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^> (cashFlowsGenerated);
        _CcashFlowsGenerated->Lock();
        INativeMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _NCIcashFlowsGenerated = _CcashFlowsGenerated->getFeature (NativeFeature::Value);
        CMarketModelMultiProductCashFlowMatrix^ _NCcashFlowsGenerated = safe_cast<CMarketModelMultiProductCashFlowMatrix^>(_NCIcashFlowsGenerated);
        std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >& _cashFlowsGenerated = static_cast<std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >&> (_NCcashFlowsGenerated->GetReference ());
    	bool _rv = (bool)(*_ppCallSpecifiedMultiProduct)->nextTimeStep ( _currentState,  _numberCashFlowsThisStep,  _cashFlowsGenerated );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
        if (_CnumberCashFlowsThisStep != nullptr) _CnumberCashFlowsThisStep->Unlock();
        if (_CcashFlowsGenerated != nullptr) _CcashFlowsGenerated->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::ICallSpecifiedMultiProduct^ Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::DisableCallability::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppCallSpecifiedMultiProduct)->disableCallability ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::ICallSpecifiedMultiProduct^ Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::EnableCallability::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppCallSpecifiedMultiProduct)->enableCallability ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppCallSpecifiedMultiProduct)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCallSpecifiedMultiProduct)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCallSpecifiedMultiProduct)->numberOfProducts ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppCallSpecifiedMultiProduct)->possibleCashFlowTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IMarketModelMultiProduct^ Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::Rebate::get ()
{
    try
    {
    	QuantLib::MarketModelMultiProduct& _rv = (QuantLib::MarketModelMultiProduct&)(*_ppCallSpecifiedMultiProduct)->rebate ( );   
        Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::ICallSpecifiedMultiProduct^ Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppCallSpecifiedMultiProduct)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::SuggestedNumeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppCallSpecifiedMultiProduct)->suggestedNumeraires ( );   
        Cephei::IVector<UInt64>^ _nrv = gcnew CoVector<UInt64> (gcnew CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IMarketModelMultiProduct^ Cephei::QL::Models::Marketmodels::Products::Multistep::CCallSpecifiedMultiProduct::Underlying::get ()
{
    try
    {
    	QuantLib::MarketModelMultiProduct& _rv = (QuantLib::MarketModelMultiProduct&)(*_ppCallSpecifiedMultiProduct)->underlying ( );   
        Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CMarketModelMultiProduct (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

